<?php
/**********************
**unit_test.inc
**Date Created: 4/13/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/

/***********
Unit tests are executed by pushing onto an array a test function to call
Includes a hash map of arguments as well as whether or not the function is expected to succeed
Called function returns true/false for success/failure and an error string argument is set on failure 

Titles are added to test cases by supplying single sized test cased arrays with only a title string
***********/
class UnitTest
{
	function runTest()
	{
		//Return false as a safety, should define something more meaningful
		return false;
	}

	//Standard print function
	function markTestCase($testSucceeded, $expectSuccess, $error)
	{
		if( $expectSuccess )
		{
			$caseSucceeded = $testSucceeded;
		}
		else
		{
			//We are expecting the inverse
			$caseSucceeded = !$testSucceeded;
		}

		if( $caseSucceeded )
		{
			//Green color
			//echo "<FONT color='#00FF00'>Passed</FONT>: result expected\n";
			echo "<B>Passed</B>: result expected\n";
		}
		else
		{
			//Red color
			echo "<FONT color='#FF0000'><B>FAILED</B></FONT>: result not expected!\n";
		}
		echo "<BR>";

		//Print the failure even if it was expected
		if( !$testSucceeded )
		{
			echo "Cause: " . $error;
			echo "<BR>";
		}
		
		echo "<BR>\n";
	}

	//Used to execute the tests once prepared
	function runTests($tests)
	{
		$succeeded = true;
		//Run tests
		foreach( $tests as $value )
		{
			//Single element entries are titles
			if( sizeof($value) == 1 )
			{
				echo $value[0];
				continue;
			}
		
			$funcName = $value[0];
			//We do positive and negative testing
			$expectSuccess = $value[1];
			$args = $value[2];

			echo "Test: " . $funcName;
			echo "<BR>\n";
			foreach( $args as $key => $value )
			{
				//Tab it over
				echo "&nbsp;&nbsp;&nbsp;&nbsp;";
				echo "key: $key, value: $value<BR>";
			}

			$error = '';

			//Get a true/false return code by calling a unit test member function
			//It should return a failure string in $error
			$succeededLocal = $this->$funcName($args, $error);
			if( $expectSuccess )
			{
				$succeeded = $succeeded && $succeededLocal;
			}
			else
			{
				$succeeded = $succeeded && !$succeededLocal;
			}
			$this->markTestCase($succeededLocal, $expectSuccess, $error);
		}
		return $succeeded;
	}
}
?>
